package org.jypj.dev.util;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import sun.misc.BASE64Encoder;

public class PictureUtils {
	/**
	 * 添加图片水印
	 * 
	 * @param srcImg
	 *            目标图片路径，如：C:\\kutuku.jpg
	 * @param waterImg
	 *            水印图片路径，如：C:\\kutuku.png
	 * @param x
	 *            水印图片距离目标图片左侧的偏移量，如果x<0, 则在正中间
	 * @param y
	 *            水印图片距离目标图片上侧的偏移量，如果y<0, 则在正中间
	 * @param alpha
	 *            透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
	 * @throws IOException
	 */
	public final static void addWaterMark(String srcImg, String waterImg, int x, int y, float alpha)
			throws IOException {
		// 加载目标图片
		File file = new File(srcImg);
		String ext = srcImg.substring(srcImg.lastIndexOf(".") + 1);
		Image image = ImageIO.read(file);
		int width = image.getWidth(null);
		int height = image.getHeight(null);

		// 将目标图片加载到内存。
		BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bufferedImage.createGraphics();
		g.drawImage(image, 0, 0, width, height, null);

		// 加载水印图片。
		Image waterImage = ImageIO.read(new File(waterImg));
		int width_1 = waterImage.getWidth(null);
		int height_1 = waterImage.getHeight(null);
		// 设置水印图片的透明度。
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

		// 设置水印图片的位置。
		int widthDiff = width - width_1;
		int heightDiff = height - height_1;
		if (x < 0) {
			x = widthDiff / 2;
		} else if (x > widthDiff) {
			x = widthDiff;
		}
		if (y < 0) {
			y = heightDiff / 2;
		} else if (y > heightDiff) {
			y = heightDiff;
		}

		// 将水印图片“画”在原有的图片的制定位置。
		g.drawImage(waterImage, x, y, width_1, height_1, null);
		// 关闭画笔。
		g.dispose();

		// 保存目标图片。
		ImageIO.write(bufferedImage, ext, file);
	}

	/**
	 * 读取图片前两个字节
	 * 
	 * @param src
	 * @return
	 */
	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder();
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF; // byte to int
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	/**
	 * @Description: 根据图片地址转换为base64编码字符串
	 * @Author:
	 * @CreateTime:
	 * @return
	 */
	public static String getImageStr(String imgFile) {
		InputStream inputStream = null;
		byte[] data = null;
		try {
			inputStream = new FileInputStream(imgFile);
			data = new byte[inputStream.available()];
			inputStream.read(data);
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 加密
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);
	}

	/**
	 * 通过判断图片的宽度和高度来确定是否是图片
	 * 
	 * @param imageFile
	 * @return
	 */
	public static boolean isImage(File imageFile) {
		if (!imageFile.exists()) {
			return false;
		}
		Image img = null;
		try {
			img = ImageIO.read(imageFile);
			if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
				return false;
			}
			return true;
		} catch (Exception e) {
			return false;
		} finally {
			img = null;
		}
	}

	public static void main(String[] args) throws IOException {
		// addWaterMark("D:/Koala.jpg", "D:/555.png", -1, -1, 0.5f);
		// System.out.println(isImage(new File("D:/555.png")));
	}
}
